<?php


namespace App\Model;


use App\Tool\Request;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Redis;

/**
 * App\Model\SnappedSku
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $snapped_id 秒杀表id
 * @property int $type 秒杀类型,1是固定价格，2是折扣
 * @property float $price 秒杀价格
 * @property float $percent 折扣，单位折
 * @property int $sku_id goods_sku表的id
 * @property int $goods_id 商品id
 * @property-read mixed $all
 * @property-read mixed $num
 * @property-read mixed|string $real_price
 * @property-read \App\Model\Goods $goods
 * @property-read \App\Model\GoodsSku $goods_sku
 * @property-read \App\Model\Snapped $snapped
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku wherePercent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereSkuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereSnappedId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\SnappedSku whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SnappedSku extends Base
{

    protected $table = 'snapped_sku';

    protected $fillable = ['snapped_id', 'type', 'price', 'percent', 'sku_id', 'goods_id','disable'];

    protected $appends = ['num', 'all','real_price'];

    /**
     * 秒杀剩余库存
     * Create by PeterYang
     * 2020/06/05 20:29:00
     * Email:904801074@qq.com
     * @param $value
     * @return mixed
     */
    function getNumAttribute($value)
    {

        $goods_sku_id = $this->attributes['sku_id'];

        return Redis::get('snapped:num_goods_sku_id_' . $goods_sku_id);

    }

    /**
     * 写入时的库存
     * Create by PeterYang
     * 2020/06/06 14:52:35
     * Email:904801074@qq.com
     * @param $value
     * @return mixed
     */
    function getAllAttribute($value)
    {

        $goods_sku_id = $this->attributes['sku_id'];

        return Redis::get('snapped:all_goods_sku_id_' . $goods_sku_id);

    }


    function snapped()
    {


        return $this->belongsTo(Snapped::class, 'snapped_id', 'id');
    }

    function goods_sku()
    {

        return $this->belongsTo(GoodsSku::class, 'sku_id', 'id');
    }

    function goods()
    {


        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }

    /**
     * 实际价格
     * Create by Peter
     * 2020/06/11 17:11:01
     * Email:904801074@qq.com
     * @return mixed|string
     */
    function getRealPriceAttribute()
    {

        try {

            $goods_sku=$this->getRelations()['goods_sku'];

            if(!$goods_sku) return '';

            $price = '';

            $type = $this->attributes['type'];

            //固定价格
            if ($type == 1) $price = $this->attributes['price'];

            //折扣
            if ($type == 2) $price = number_format($goods_sku->price * ($this->attributes['percent'] / 10), 2);

            return $price;

        } catch (\Exception $exception) {


            return '';

        }


    }

    static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        //小程序(前端)模块添加必须上架的限制
        static::addGlobalScope('disable', function (Builder $builder) {



            $mode=Request::getMode();

            if (strtolower($mode) == strtolower('weapp')) {

                $builder->where('disable', 1);
            }

        });
    }


}
